package com.gqzdev.learn.controller;

import com.gqzdev.learn.model.Collect;
import com.gqzdev.learn.model.Student;
import com.gqzdev.learn.service.CollectService;
import com.gqzdev.learn.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * (Collect)表控制层
 *
 * @author ganquanzhong
 * @since 2020-04-18 22:02:00
 */
@Controller
public class CollectController {

    @Autowired
    private CollectService collectService;

    @Autowired
    private CourseService courseService;


    //加入课程
    @RequestMapping("/addCourse/{id}")
    public ModelAndView addCourse(HttpSession session,@PathVariable("id") Integer id) {
        Student student = (Student) session.getAttribute("user");
        ModelAndView mav = new ModelAndView();
        HashMap<String , Object> map = new HashMap<>();
        map.put("stuId",student.getId());
        map.put("cId", id);
        collectService.saveCollect(map);
        mav.setViewName("redirect:/searchMyCourseInfo.html");
        return mav;
    }

    //我的课程
    @RequestMapping("/searchMyCourseInfo")
    public ModelAndView searchMyCourseInfo(HttpSession session) {
        ModelAndView mav = new ModelAndView();
        Student student = (Student) session.getAttribute("user");
        mav.addObject("courses", courseService.searchMyCourse(student.getId()));
        mav.setViewName("searchMyCourseInfo");
        return mav;
    }

    @RequestMapping("/deleteMyCourse/{id}")
    public ModelAndView deleteMyCourse(HttpSession session, @PathVariable("id") Integer id) {
        Student student = (Student) session.getAttribute("user");
        ModelAndView mav = new ModelAndView();
        HashMap<String , Object> map = new HashMap<>();
        map.put("stuId",student.getId());
        map.put("cId", id);
        collectService.deleteCollect(map);
        mav.setViewName("redirect:/searchMyCourseInfo.html");
        return mav;
    }

}